package libra.tracking.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

import org.apache.skywalking.apm.toolkit.trace.CallableWrapper;
import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper;
import org.apache.skywalking.apm.toolkit.trace.SupplierWrapper;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.apache.skywalking.apm.toolkit.trace.TraceCrossThread;
import org.springframework.stereotype.Service;

@Service
public class ThreadOneService {
    public static class MyCallable<String> implements Callable<String> {
        @Override
        public String call() throws Exception {
            return null;
        }
    }
    @Trace
	public void doThread() {
		 ExecutorService executorService = Executors.newFixedThreadPool(1);
		 executorService.submit(new MyCallable());
	}
    
    //
    @TraceCrossThread
    public static class MyCallable1<String> implements Callable<String> {
        @Override
        public String call() throws Exception {
            return null;
        }
    }
    @Trace
	public void doThread1() {
		 ExecutorService executorService = Executors.newFixedThreadPool(1);
		 executorService.submit(new MyCallable1());
	}
    
    //
    @Trace
	public void doThread2() {
		ExecutorService executorService = Executors.newFixedThreadPool(1);
		executorService.submit(CallableWrapper.of(new Callable<String>() {
			@Override
			public String call() throws Exception {
				return null;
			}
		}));
		executorService.execute(RunnableWrapper.of(new Runnable() {
	        @Override public void run() {
	        }
	    }));
		CompletableFuture.supplyAsync(SupplierWrapper.of(new Supplier<Object>() {
			@Override
			public Object get() {
				return null;
			}
		})).thenAccept(System.out::println);
	}
}
